package com.hxl.access.service.impl;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxl.access.entity.Realdata;
import com.hxl.access.mapper.RealDataMapper;
import com.hxl.access.service.RealDataService;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 功能描述
 *
 * @author: hxl
 * @date: 2023年12月31日 13:24
 */
@Service
@DS("db1")
public class RealDataServiceImpl extends ServiceImpl<RealDataMapper, Realdata> implements RealDataService {

    @Override
    public List<Realdata> findRealDataAll() {
        return this.list();
    }

    @Override
    public String getPoint() {
        List<Realdata> list = this.findRealDataAll();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            for (int j = 2; j < 10; j++) {
                sb.append(list.get(i).getChannelId()+"MN");
                // 获取编码及单位 [0] 是编码 [1]是单位，
                Field[] fields = list.get(i).getClass().getDeclaredFields();
                String[] codeAndUnit = getCodeAndUnit(fields[j].getName());
                sb.append(codeAndUnit[0] + ";");
                sb.append(list.get(i).getPosition() + ";");
                sb.append(fields[j].getName().toUpperCase() + ";");
                sb.append(list.get(i).getCh4() + ";");
                sb.append(codeAndUnit[1] + ";");
                sb.append("0" + ";");
                sb.append(DateUtils.format(list.get(i).getTime(), "yyyy-MM-dd HH:mm:ss") + "~");
            }
            sb.append("||");
        }
        return sb.toString();
    }

    /**
     * 根据描述信息 ch4 返回对应编码及单位
     * @param describe
     * @return
     */
    public String[] getCodeAndUnit(String describe) {
        String[] codeAndUnit = new String[2];
        switch (describe) {
            case "ch4":
                codeAndUnit[0] = "8001";
                codeAndUnit[1] = "%";
                break;
            case "co":
                codeAndUnit[0] = "8002";
                codeAndUnit[1] = "ppm";
                break;
            case "o2":
                codeAndUnit[0] = "8003";
                codeAndUnit[1] = "%";
                break;
            case "c2h2":
                codeAndUnit[0] = "8004";
                codeAndUnit[1] = "ppm";
                break;
            case "no":
                codeAndUnit[0] = "8005";
                codeAndUnit[1] = "";
                break;
            case "co2":
                codeAndUnit[0] = "8006";
                codeAndUnit[1] = "%";
                break;
            case "c2h4":
                codeAndUnit[0] = "8007";
                codeAndUnit[1] = "ppm";
                break;
            case "c2h6":
                codeAndUnit[0] = "8008";
                codeAndUnit[1] = "ppm";
                break;
            case "n2":
                codeAndUnit[0] = "8009";
                codeAndUnit[1] = "%";
                break;
            case "h2":
                codeAndUnit[0] = "8010";
                codeAndUnit[1] = "%";
                break;
            case "t":
                codeAndUnit[0] = "8101";
                codeAndUnit[1] = "°C";
                break;
            case "dp":
                codeAndUnit[0] = "8102";
                codeAndUnit[1] = "pa";
                break;
            default:
                break;
        }
        return codeAndUnit;
    }
}
